﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    /// <summary>
    /// WorkBreakdownStructureTreePanel.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class WorkBreakdownStructureTreePanel : UserControl
    {
        public WorkBreakdownStructureTreePanel()
        {
            InitializeComponent();
        }

        private void OnWorkBreakdownStructureSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            WorkBreakdownStructureViewModel _structure = treeViewStructure.SelectedItem as WorkBreakdownStructureViewModel;
            if (null != _structure)
            {
                WorkBreakdownStructureGroupViewModel _group = this.DataContext as WorkBreakdownStructureGroupViewModel;
                if (null != _group) _group.WorkBreakdownStructure = _structure;
            }
        }

        private void OnCanExecuteNewCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
        private void OnExecuteNewCommand(object sender, ExecutedRoutedEventArgs e)
        {
            WorkBreakdownStructureGroupViewModel _context = this.DataContext as WorkBreakdownStructureGroupViewModel;
            WorkBreakdownStructurePanel _panel = new WorkBreakdownStructurePanel();
            CommonDialog _dialog = new CommonDialog(_panel);
            _dialog.Title = "YWBS 생성";
            _dialog.Height = 180;
            if (true == _dialog.ShowDialog())
            {
                WorkBreakdownStructureViewModel _newly=_panel.DataContext as WorkBreakdownStructureViewModel;
                ConfigurationViewModel _configuration = ConfigurationViewModel.GetInstance();
                if (_context.Name == _configuration.Object)
                {
                    ObjectViewModel _object = new ObjectViewModel();
                    _object.Code = _newly.Code;
                    _object.Name = _newly.Name;
                    _object.Group = _context;
                    if (null == _context.WorkBreakdownStructure) _context.WorkBreakdownStructures.Add(_object);
                    else _context.WorkBreakdownStructure.AddChild(_object);
                }
                else
                {
                    _newly.Group = _context;
                    if (null == _context.WorkBreakdownStructure) _context.WorkBreakdownStructures.Add(_newly);
                    else _context.WorkBreakdownStructure.AddChild(_newly);
                }
            }
        }

        private void OnCanExecuteReplaceCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            WorkBreakdownStructureGroupViewModel _context = this.DataContext as WorkBreakdownStructureGroupViewModel;
            e.CanExecute = (null == _context) ? false : null != _context.WorkBreakdownStructure;
        }
        private void OnExecuteReplaceCommand(object sender, ExecutedRoutedEventArgs e)
        {
            WorkBreakdownStructureGroupViewModel _context = this.DataContext as WorkBreakdownStructureGroupViewModel;
            WorkBreakdownStructurePanel _panel = new WorkBreakdownStructurePanel();
            _panel.DataContext = _context.WorkBreakdownStructure;
            CommonDialog _dialog = new CommonDialog(_panel);
            _dialog.Title = "YWBS 수정";
            _dialog.Height = 180;
            if (true == _dialog.ShowDialog()) { }
        }

        private void OnCanExecuteRemoveCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            WorkBreakdownStructureGroupViewModel _context = this.DataContext as WorkBreakdownStructureGroupViewModel;
            e.CanExecute = (null == _context) ? false : null != _context.WorkBreakdownStructure;
        }
        private void OnExecuteRemoveCommand(object sender, ExecutedRoutedEventArgs e)
        {
            WorkBreakdownStructureGroupViewModel _context = this.DataContext as WorkBreakdownStructureGroupViewModel;
            if (null == _context.WorkBreakdownStructure.Parent) _context.WorkBreakdownStructures.Remove(_context.WorkBreakdownStructure);
            else _context.WorkBreakdownStructure.Parent.Children.Remove(_context.WorkBreakdownStructure);
        }

        private void OnCanExecuteComponentsCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            WorkBreakdownStructureGroupViewModel _context = this.DataContext as WorkBreakdownStructureGroupViewModel;
            e.CanExecute = (null == _context) ? false : null != _context.WorkBreakdownStructure as ObjectViewModel;
        }
        private void OnExecuteComponentsCommand(object sender, ExecutedRoutedEventArgs e)
        {
            WorkBreakdownStructureGroupViewModel _context = this.DataContext as WorkBreakdownStructureGroupViewModel;
            if (null != _context.WorkBreakdownStructure)
            {
                ObjectViewModel _object = _context.WorkBreakdownStructure as ObjectViewModel;
                ObjectRatioPanel _panel = new ObjectRatioPanel();
                _panel.DataContext = _object;
                CommonDialog _dialog = new CommonDialog(_panel);
                _dialog.Title = "부재 비율";
                _dialog.Width = 800;
                _dialog.Height = 600;
                _dialog.ShowDialog();
            }
        }
    }
}